package com.lihao.sam.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.CompanyEmpDao;
import com.lihao.sam.dao.EmpCountScoreDao;
import com.lihao.sam.dao.EmpInputScoreDao;
import com.lihao.sam.dao.EmpSettingDao;
import com.lihao.sam.dto.CompanyEmpCountDto;
import com.lihao.sam.entity.*;
import com.lihao.sam.service.intf.CompanyEmpService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @ClassName CompanyEmpServiceImpl
 * @Description TODO
 * @Author LiHao
 * @Date 2022/11/22 22:54
 * @Version 1.0
 */
@Service
public class CompanyEmpServiceImpl implements CompanyEmpService {
    @Resource
    private CompanyEmpDao dao;
    @Resource
    private EmpSettingDao dao1;
    @Resource
    private EmpInputScoreDao dao2;
    @Resource
    private EmpCountScoreDao dao3;

    @Override
    public R addTX(CompanyEmp companyEmp) {
        if (dao.add(companyEmp) > 0) {
            EmpSetting empSetting = new EmpSetting(companyEmp.getWorkId(), companyEmp.getEmpName(), companyEmp.getGender(), "***", "***", "***", "***", 0.0);
            EmpInputScore empInputScore = new EmpInputScore(companyEmp.getWorkId(), companyEmp.getEmpName(), new Date(), "***", 0, 0);
            EmpCountScore empCountScore = new EmpCountScore(companyEmp.getWorkId(), companyEmp.getEmpName(), 0, 0);
            if (dao1.add(empSetting) > 0 && dao2.add(empInputScore) > 0 && dao3.add(empCountScore) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public int addBatchTX(List<CompanyEmp> companyEmpList) {
        if (dao.addBatch(companyEmpList) > 0) {
            for (CompanyEmp companyEmp : companyEmpList) {
                EmpSetting empSetting = new EmpSetting(companyEmp.getWorkId(), companyEmp.getEmpName(), companyEmp.getGender(), "***", "***", "***", "***", 0.0);
                EmpInputScore empInputScore = new EmpInputScore(companyEmp.getWorkId(), companyEmp.getEmpName(), new Date(), "***", 0, 0);
                EmpCountScore empCountScore = new EmpCountScore(companyEmp.getWorkId(), companyEmp.getEmpName(), 0, 0);
                dao1.add(empSetting);
                dao2.add(empInputScore);
                dao3.add(empCountScore);
            }
        }
        return 1;
    }

    @Override
    public R updateTX(CompanyEmp companyEmp) {
        if (dao.update(companyEmp) > 0) {
            EmpSetting empSetting = new EmpSetting(companyEmp.getId(), companyEmp.getWorkId(), companyEmp.getEmpName(), companyEmp.getGender());
            EmpInputScore empInputScore = new EmpInputScore(companyEmp.getId(), companyEmp.getWorkId(), companyEmp.getEmpName());
            EmpCountScore empCountScore = new EmpCountScore(companyEmp.getId(), companyEmp.getWorkId(), companyEmp.getEmpName());
            if (dao1.updateByCompanyEmp(empSetting) > 0 && dao2.updateByCompanyEmp(empInputScore) > 0 && dao3.updateByCompanyEmp(empCountScore) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R deleteTX(int id) {
        //工作证号=员工编号
        //通过id查询物业人员的信息，此时获得物业人员工作证号
        CompanyEmp companyEmp = dao.selectOneById(id);
        if (companyEmp != null) {
            //删除物业员工表中的记录
            dao.delete(id);
            //在通过物业人员的工作证号去找到员工表中是否存在该员工的详细信息
            EmpSetting empSetting = dao1.selectOneByEmpNo(companyEmp.getWorkId());
            EmpInputScore empInputScore = dao2.selectOneByEmpNo(companyEmp.getWorkId());
            EmpCountScore empCountScore = dao3.selectOneByEmpNo(companyEmp.getWorkId());
            if (empSetting != null && empInputScore != null && empCountScore != null) {
                //如果找到则通过员工编号，删除该条员工信息，未找到则不删除
                if (dao1.deleteByEmpNo(companyEmp.getWorkId()) > 0 && dao2.deleteByEmpNo(companyEmp.getWorkId()) > 0 && dao3.deleteByEmpNo(companyEmp.getWorkId()) > 0) {
                    return R.ok();
                }
            } else {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public PageVo queryPage(int page, int limit, String workId, String empName, String houseId) {
        PageHelper.startPage(page, limit);
        PageInfo<CompanyEmp> pageInfo = new PageInfo<>(dao.selectByWhere(workId, empName, houseId));
        return new PageVo(0, "OK", pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 物业人员信息上传Excel实现批量添加，同时使员工基本资料、员工分数录入、员工分数统计也完成批量添加对应员工
     */
    @Override
    public R uploadExcel(MultipartFile file) throws IOException {
        if (file != null) {
            InputStream inputStream = file.getInputStream();
            List<CompanyEmp> list = EasyExcel.read(inputStream, CompanyEmp.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            if (addBatchTX(list) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public R deleteBatch(int[] ids) {
        if (dao.delBatch(ids) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    /** 统计报表 */
    @Override
    public R queryCompanyEmpCount() {
        //1.查询
        List<CompanyEmpCountDto> list = dao.selectCompanyEmpCount();
        //2.组装结果数据
        Map<String, ArrayList> map = new HashMap<>();
        map.put("companyName", new ArrayList());
        map.put("num", new ArrayList());
        for (CompanyEmpCountDto dto : list) {
            map.get("companyName").add(dto.getCompanyName());
            map.get("num").add(dto.getNum());
        }
        return R.ok(map);
    }
}
